<?php
/**
 * User: wanghousheng
 * Date: 2017/8/25
 * Time: 17:48
 * 创蓝短信接口
 */
require_once dirname(__FILE__) . '/Clmsm.php';
class Sms_Lite extends Clmsm {
    private $config;
    private $time = 60;//发送时间频率s
    private $sign = "【校创科技】";//签名
    public function __construct($config){
        $this->config = $config;
        isset($config['sign'])?$this->config['sign']=$config['sign']:$this->config['sign'] = $this->sign;
        if (isset($config['time']) && intval($config['time']))$this->time = $config['time'];
        parent::__construct($this->config);
    }
    /**
     * Name: 发送短信
     * User: wanghousheng
     * Date: 2016/12/27
     * @param $mobile
     * @param $type 发送短信类型
     */
    public function send_code($mobile,$type=4,$code=''){
        if ($mobile){
            if (empty($code))$code = rand(100000,999999);
            $content = $this->message($type).$code;
            $cache = PhalApi\DI()->cache->get($mobile.'codetime');
            if (isset($cache) && $cache){
                $code_bend = time()-intval($cache);
                if ($code_bend<$this->time){
                    return $this->ApiState(false,'短信发送频率太快，请稍候再试...');
                }else{
                    PhalApi\DI()->cache->delete($mobile.'codetime');
                }
            }
            $res = $this->send($mobile,$content);
            if ($res){
                //保存验证码
                $send['send_time'] = time();
                $send['send_code'] = $code;
                $send['type'] = $type;
                if (isset($this->config['expire']) && !empty(intval($this->config['expire']))){
                    $time=$this->config['expire'];
                }else{
                    $time=60*5;
                }
                PhalApi\DI()->cache->set($mobile.'codetime',time(),$this->time);
                PhalApi\DI()->cache->set($mobile.$code,$send,$time);
                return $this->ApiState(true,'发送成功');
            }else{
                return $this->ApiState(false,'发送失败');
            }
        }
    }

    /**
     * Name: 检查验证码
     * User: wanghousheng
     * Date: 2016/12/27
     * @param $mobile
     * @param $code
     * @return bool
     */
    public function check_code($mobile,$code){
        if ($mobile && $code){
            $send = PhalApi\DI()->cache->get($mobile.$code);
            if ($send && $send['send_code']==$code){
                PhalApi\DI()->cache->delete($mobile.$code);
                PhalApi\DI()->cache->delete($mobile.'codetime');
                return true;
            }
        }
        return false;
    }
    /**
     * Name: 短信模板
     * User: wanghousheng
     * Date: 2017/6/14
     * @param int $type 1注册2修改密码
     * @return string
     */
    private function message($type = 4)
    {
        $content = '您当前操作的验证码为：';
        switch ($type) {
            case 1:
                $content = '您于' . date('Y-m-d H:i:s', time()) . '修改密码的验证码为：';
                break;
            case 2:
                $content = '您注册的验证码为：';
                break;
            case 3:
                $content = '您于' . date('Y-m-d H:i:s', time()) . '设置支付密码的验证码为：';
                break;
            case 4:
                $content = '您当前操作的验证码为：';
                break;
            case 5:
                $content = '您的商家账号于' . date('Y-m-d H:i:s', time());
                break;
        }
        return $this->sign . $content;
    }
}